package com.zjlanyun.eplus;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.blankj.utilcode.utils.ToastUtils;
import com.zjlanyun.eplus.utils.SPData;
import com.zjlanyun.eplus.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class Home extends Activity {

    @Bind(R.id.version)
    TextView version;

    private Context mContext = this;
    private ArrayList<HashMap<String, Object>> usertypeList;
    private SimpleAdapter usertypeAdapter;
    private AlertDialog dialog;
    public static Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        ButterKnife.bind(this);
        StatusBarUtil.transparencyBar(this);

        mActivity = this;


        // 显示版本号
        PackageManager mPackageManager = this.getPackageManager();
        try {
            PackageInfo mPackageInfo = mPackageManager.getPackageInfo(this.getPackageName(), 0);
            version.setText("Ver:" + mPackageInfo.versionName);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        // 用户类型列表
        usertypeList = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < Data.USER_TYPE.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("name", Data.USER_TYPE[i]);
            map.put("usertype", i + 1);
            usertypeList.add(map);
        }
        usertypeAdapter = new SimpleAdapter(mContext, usertypeList, R.layout.home_usertype_item,
                new String[]{"name"}, new int[]{R.id.name});
    }

    /**
     * 演示
     */
    @OnClick(R.id.demo)
    public void demoClick() {
        ToastUtils.showShortToast("待开发");
//        SPData.getUserinfo().putBoolean("isDemo", true);
//        SPData.getUserinfo().putInt("usertype", Data.USER_TYPE_INTERIOR);
//        SPData.getUserinfo().putString("serverUrlDemo", "123.157.241.34:8321");
//        //配置演示用户信息，进行登录和获取配置
//
//        Intent intent = new Intent(Home.this, MenuActivity.class);
//        startActivity(intent);
//        finish();
    }

    /**
     * 登录
     */
    @OnClick(R.id.login)
    public void loginClick() {
        // 设置演示为false
        SPData.getUserinfo().putBoolean("isDemo", false);
        // 判断是否已经有设置过业务地址
        if (SPData.getUserinfo().getString("serverUrl", "").equals("")) {
            Intent intent = new Intent(Home.this, ConnectSetup.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(Home.this, Login.class);
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
